package Controller.Music;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import Bean.JoinBean;
import Bean.MusicBean;
import Bean.rowBean;
import Dao.MusicDao;

public class AlbumList implements Controller {
	private MusicDao dao;

	public void setDao(MusicDao dao) {
		this.dao = dao;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse arg1) throws Exception {
		String album = request.getParameter("album");
		// ------------최신 1달간 발매된 노래 찾기------------------
		Calendar cal = java.util.Calendar.getInstance();
		// 현재 년도, 월, 일
		String year = Integer.toString(cal.get(cal.YEAR));
		String month = Integer.toString(cal.get(cal.MONTH) + 1);
		month = month.length() > 1 ? month : "0" + month;
		String day = Integer.toString(cal.get(cal.DAY_OF_MONTH));
		day = day.length() > 1 ? day : "0" + day;

		String nowYMD = year + month + day; // 현재 yyyymmdd

		cal.add(cal.WEEK_OF_MONTH, -32); // 4주전 시간

		year = Integer.toString(cal.get(cal.YEAR));
		month = Integer.toString(cal.get(cal.MONTH) + 1);
		month = month.length() > 1 ? month : "0" + month;
		day = Integer.toString(cal.get(cal.DAY_OF_MONTH));
		day = day.length() > 1 ? day : "0" + day;

		String agoYMD = year + month + day; // 4주전 시간

		int intNowYMD = Integer.parseInt(nowYMD);
		int intAgoYMD = Integer.parseInt(agoYMD);
		// ------------------------------------------------------------------------------

		rowBean row = new rowBean();

		row.setIntAgoYMD(intAgoYMD);
		row.setIntNowYMD(intNowYMD);

		ModelAndView mav = new ModelAndView();
		
		List<MusicBean> albumList = null;
		
		if (album.equals("nalbum")) {
			albumList = dao.GetNewAlbum(row);
		} else if (album.equals("balbum")) {
			albumList = dao.GetBalladeAlbum();
		} else if (album.equals("dalbum")) {
			albumList = dao.GetDanceAlbum();
		} else if (album.equals("halbum")) {
			albumList = dao.GetHiphopAlbum();
		} else if (album.equals("ralbum")) {
			albumList = dao.GetRockAlbum();
		} else if (album.equals("oalbum")) {
			albumList = dao.GetOSTAlbum();
		}

		
		mav.setViewName("albumArt");
		
		mav.addObject("albumList", albumList);
		return mav;
	}

}
